/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mainclasses;

import ruter.map.AnonymousMapComponent;
import ruter.map.Map;
import ruter.visual.RoundBall;
import ruter.simulator.Simulation;
import ruter.simulator.Updatable;
import ruter.simulator.Vehicle;

import java.awt.Color;
import java.util.Random;

import components.RobotPilot;

import ruter.network.AdhocNetwork;

/**
 *
 * @author camilo
 */
public class Benchmarking implements Updatable {

	public static final int[] MAP_DIMENSION = {1000, 600};
	public static final int NODE_RADIUS = 90;
	public static final int NUMBER_OF_ROBOTS = 25;
	public static final long FRAME_TIME = Simulation.ACCURACY_HIGH;
	public static final int MAX_PPM = 10;
	public static final int MIN_PPM = 1;

	private Map map;
	private AdhocNetwork network;
	private Simulation simulation;
	private boolean findOrNot, allFind;
	private long timeLastFound;

	public static void main(String args[]) {
		Benchmarking test = new Benchmarking();
	}

	public Benchmarking() {

		// Map
		map = new Map(MAP_DIMENSION[0], MAP_DIMENSION[1], Double.MAX_VALUE);

		// First simulation
		restartSimulation();
	}

	public void restartSimulation() {

		// Restarting flags
		findOrNot = false;
		allFind = false;
		timeLastFound = 0;

		// Network
		network = new AdhocNetwork(NODE_RADIUS);

		// Simulation
		simulation = new Simulation(null, map, network, FRAME_TIME);
		simulation.setMaximumSimulationSpeed();
		simulation.addUpdatableElementToThread(this);

		// Adding new robots
		addRobots();

		// Start simulation
		simulation.start();
	}

	private void addRobots() {
		Random random = new Random();
		AnonymousMapComponent target = new AnonymousMapComponent(map.getXBounds()/2, map.getYBounds()/2);
		for (int i = 0; i < NUMBER_OF_ROBOTS; i++) {
			Vehicle v = new Vehicle(10 + i * 5, 10, 2, 1, 1, 180);
			RobotPilot pilot = new RobotPilot(map, target, FRAME_TIME/1000d,simulation);
			pilot.setUseRules(true);
			v.putDriver(pilot);
//			v.putMap(map);
			network.addNode(pilot);
			simulation.addVehicle(v, new Color(random.nextInt()));
		}
	}

	int count = 0;
	@Override
	public void update(Simulation simulation) {
		if(simulation.getRealTime() < 0) {
			System.out.println("  ---");
			simulation.stopSimulation();
			restartSimulation();
		} else {
			int robotsFoundTarget = 0;
			for (int i = 0; i < simulation.getVehicles().size(); i++) {
				RobotPilot robot = (RobotPilot) simulation.getVehicles().get(i).getDriver();
				if(robot.getTimeToFind()!=0){
					if(!findOrNot) {
						System.out.print(""+(robot.getTimeToFind()/1000d));
						findOrNot = true;
					}
					robotsFoundTarget ++;
				}
			}
			robotsFoundTarget -= simulation.getVehicles().size();
			if(robotsFoundTarget == 0 && !allFind) {
				System.out.println(""+(simulation.getRealTime()/1000d));
				allFind = true;
				count ++;
				if(count > 30) {
					System.exit(0);
				}
				simulation.stopSimulation();
				restartSimulation();
			} else if(robotsFoundTarget == -1) {
				if(timeLastFound == 0) {
					timeLastFound = simulation.getRealTime();
					System.out.print(" | ");
				} else if(simulation.getRealTime() - timeLastFound > 7200000) {
					System.out.println("Inf");
					simulation.stopSimulation();
					restartSimulation();
				}
			}
		}
	}

	@Override
	public void update(double x, double y) {
	}
}
